# coding=utf-8

import time
import pika


connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

# 声明 queue，并指定持久存储
channel.queue_declare(queue='task_queue', durable=True)
print ' [*] Waiting for messages. To exit press CTRL+C'

# 定义回调函数，该函数会在退出前向 Server 发送 ack
def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)
    time.sleep( body.count('.') )
    print " [x] Done"
    ch.basic_ack(delivery_tag=method.delivery_tag)  # 消息确认

# 指定消息预取
channel.basic_qos(prefetch_count=1)

# 订阅消息
channel.basic_consume(callback, queue='task_queue')

channel.start_consuming()

